import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/model/room_data_model.dart';
import 'package:love_newf/app/com/model/room_info_model.dart';
import 'package:love_newf/app/com/util/my_toast.dart';
import 'package:love_newf/app/com/view/common_view.dart';
import 'package:love_newf/app/page/live/live_logic.dart';
import 'package:love_newf/generated/assets.dart';

class LivePreviewPage extends StatefulWidget {
  RoomDataRoomMais roomMais;

  LivePreviewPage(this.roomMais, {super.key});

  @override
  State<LivePreviewPage> createState() => _LivePreviewPageState();
}

class _LivePreviewPageState extends State<LivePreviewPage> {
  RoomDataRoomMais get mais => widget.roomMais;
  LiveLogic logic = Get.find<LiveLogic>();

  @override
  Widget build(BuildContext context) {
    return LStack([
      IgnorePointer(
          ignoring: true, // 禁用点击事件
          child: Platform.isAndroid
              ? AndroidView( 
                  viewType:
                      'plugins.flutter.io/live_preview_view', // 在 Android 注册的类型
                  creationParams: {'roomMais': json.encode(mais)}, // 传递的参数
                  creationParamsCodec: const StandardMessageCodec(), // 编码器
                  onPlatformViewCreated: (int id) {},
                )
              : UiKitView(
                  viewType: 'plugins.flutter.io/live_preview_view',
                  creationParams: {'roomMais': json.encode(mais.toJson())},
                  creationParamsCodec: const StandardMessageCodec(),
                )),
      LRow.eazy([
        LExpand(),
        LImage.easy(Assets.liveMaiRose, 23.w, 23.w),
        4.lsw,
        LColumn.eazy([microphone, if (mais.isAnchor() || logic.isOwner) xiaMai])
      ]).align(Alignment.topLeft).paddingAll(6.w),
      Align(
          alignment: Alignment.bottomLeft,
          child: LColumn.eazy([
            LText.easy(mais.nick, 12.w, null, Colors.white),
            LRow.eazy([
              LImage.easy(Assets.liveMaiRose, 16.w, 16.w),
              LText.easy(
                  mais.getReceiveTokens().toString(), 10.w, null, Colors.white),
            ])
          ]).padding(left: 5.w)),
      Align(alignment: Alignment.bottomRight, child: rankBox)
    ]).onTapL(() {
      logic.sendGiftDialog
          .show(userId: mais.userId.toString(), headImg: mais.avatar);
    });
  }

  get rankBox {
    var ranks = mais.getRankVOS();
    var list = [
      for (int i = 0; i < ranks.length; i++)
        Positioned(
            left: (11.w * i).toDouble(),
            child: LImage.easy(ranks[i].avatar, 22.w, 22.w, 22.w)),
    ];
    return LStack(list.reversed.toList()).size(22.w * ranks.length, 22.w);
  }

  get xiaMai => LText.easy("下麦", 10.w, null, Colors.white)
          .bgColor(const Color(0x3B000000))
          .center()
          .size(23.w, 23.w)
          .radius(23.w)
          .onTapL(() {
        CommonView.commDialog(
            title: "下麦提示",
            message: logic.isOwner ? '确定将嘉宾抱下麦吗' : '确定要下麦吗',
            confirm: () {
              Get.back();
              logic.xiaMai(mais);
            });
      });

  get microphone => LImage.easy(
              mais.maiOpen == 1
                  ? Assets.liveMicrophoneOn
                  : Assets.liveMicrophoneOff,
              23.w,
              23.w)
          .margin(bottom: 4.w)
          .onTapL(() {
        if (mais.isAnchor()) {
          // mais.maiOpen = mais.maiOpen == 1 ? 0 : 1;
          logic.settingMuteMai(mais, mais.maiOpen == 0);
          // logic.refreshMains;
        }
      });
}
